package com.caishi.lkx.exam.question;

import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.exam.model.QuestionModel;
import com.zzw.common.exception.BizRuntimeException;
import com.zzw.common.utils.MD5Util;


import java.util.List;

/**
 * 组合题
 */
public class CombinationDataProcess extends AbstractQuestionDataProcess implements Question<Object> {
    private final QuestionDataProcessComposite questionDataProcessComposite;

    public CombinationDataProcess(QuestionDataProcessComposite questionDataProcessComposite) {
        this.questionDataProcessComposite = questionDataProcessComposite;
    }

    @Override
    public double computedQualityLevel(QuestionModel questionModel) {
        List<QuestionModel> children = questionModel.getChildren();
        double score = 0;
        if(children != null){
            for (QuestionModel model : children) {
                if (model.getId().equals(questionModel.getId())) throw new BizRuntimeException("组合题嵌套自己了");
                score += questionDataProcessComposite.computedQualityLevel(model);
            }
        }
        return score;
    }

    @Override
    protected double computedAnswerQualityLevel(QuestionModel questionModel) {
        return 0;
    }

    @Override
    protected double computedRightAnswerQualityLevel(QuestionModel questionModel) {
        return 0;
    }

    @Override
    public boolean userAnswerIsRight(Object rightAnswer, Object userAnswer) {
        // 组合题的答案计算分配到各个小题
        throw new BizRuntimeException();
    }

    @Override
    public String questionContentSign(QuestionModel questionModel) {
        return StrUtil.isNotBlank(questionModel.getSourceId())?MD5Util.MD5Encode(questionModel.getSourceId()):null;
    }
}
